package com.daocao.ds.view;

import android.app.Dialog;
import android.content.Context;
import android.content.res.Configuration;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;


import com.daocao.ds.R;
import com.daocao.ds.adapter.SelectMenuAdapter;
import com.daocao.ds.api.model.KeyValueModel;
import com.daocao.ds.util.Util;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by xsooy-pc on 18-5-22.
 */

public class SelectMenuDilaog extends Dialog {


    @BindView(R.id.list)
    RecyclerView list;
    @BindView(R.id.btn_cancel)
    TextView btnCancel;
    private Context context;
    private SelectMenuAdapter adapter;
    public String menuType;

    public SelectMenuDilaog(Context context) {
        this(context, R.style.DialogStyle);
    }

    public SelectMenuDilaog(Context context, int themeResId) {
        super(context, themeResId);
        this.context = context;
        initView();
    }

    private void initView() {
        setContentView(R.layout.dialog_select_menu);
        ButterKnife.bind(this);
        setCancelable(false);

        Window mWindow = getWindow();
        WindowManager.LayoutParams lp = mWindow.getAttributes();
        lp.width = Util.getDisplayMetrics(context).widthPixels;
        lp.gravity = Gravity.BOTTOM;
        if (context.getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {// 横屏
            lp.width = Util.getDisplayMetrics(context).heightPixels;
        } else {
            lp.width = Util.getDisplayMetrics(context).widthPixels;
        }
        mWindow.setAttributes(lp);

        adapter = new SelectMenuAdapter(context);
        adapter.setOnChooseListener(new SelectMenuAdapter.OnChooseListener() {
            @Override
            public void onSuccess(int poi, KeyValueModel text) {
                if (onChooseListener != null) {
                    onChooseListener.onSuccess(poi, text,menuType);
                    dismiss();
                }
            }
        });
        list.setAdapter(adapter);
    }

    public void setData(String type, List<KeyValueModel> data) {
        menuType = type;
        adapter.update(data);
    }

    public void setData(List<KeyValueModel> data) {
        adapter.update(data);
    }

    private OnChooseListener onChooseListener;

    public SelectMenuDilaog setOnChooseListener(OnChooseListener onChooseListener) {
        this.onChooseListener = onChooseListener;
        return this;
    }

    @OnClick(R.id.btn_cancel)
    public void onViewClicked() {
        dismiss();
    }


    public interface OnChooseListener {
        void onSuccess(int poi, KeyValueModel text, String menuType);
    }
}
